let box2 = document.querySelector('.box2');
let main = document.querySelector('.main')


// 近期最受欢迎
pAjax({
    url:'/my/movie/v1/list/wish/order/coming.json',
    data:{
        ci:20,
        limit:30,
        offset:0
    }
}).then(function(res){
    res = JSON.parse(res);
    res = res.data.coming;
    daiying1(res);
    let imgs = document.querySelectorAll('img');   
    lazyload(imgs);
})
function daiying1(res){
    res.forEach((item,index)=> {
        box2.innerHTML += `
        <figure>
        <a href="./xiangqingye.html?id=${item.id}"><img data-src="${item.img.replace('/w.h','')}"/></a>
            <figcaption>
                <p>${item.nm}</p>
                <p>${item.wish}人想看</p>
                <p>${item.rt}</p>
            </figcaption>
        </figure>
        `
    });
}

//第一页
pAjax({
    url:'/my/movie/v2/list/rt/order/coming.json',
    data:{
        ci:20,
        limit:10,
    }
}).then(function(res){
    res = JSON.parse(res);
    res = res.data.coming;
    list(res);
    let imgs = document.querySelectorAll('img');   
    lazyload(imgs);
    // 滚屏函数
    main.onscroll =function(){
        lazyload(imgs);
    }
})
function list(res){
    res.forEach((item,index)=> {
        main.innerHTML += `
        <div class="content">
                <div class="left"><a href="./xiangqingye.html?id=${item.id}"><img data-src="${item.img.replace('/w.h','')}"/></a></div>
                <div class="right">
                    <p><span>${item.nm}</span><span>${item.wish}人想看</span></p>
                    <p>${item.cat}</p>
                    <p><span>${item.desc}</span><span><a><i class="iconfont iconxihuan1"></i></a>想看</span></p>
                    <p>${item.rt}</p>
                </div>
            </div> 
        `
    });
}
//第二页
//https://wx.maoyan.com/mmdb/movie/list/info.json?ci=20&movieIds=1211382,1346626,1383416,1278054,337487,1368406,93,18167,360,1298864
pAjax({
    url:'/my/movie/list/info.json',
    data:{
        ci:20,
        movieIds:"1211382,1346626,1383416,1278054,337487,1368406,93,18167,360,1298864",
    }
}).then(function(res){
    res = JSON.parse(res);
    res = res.data.movies;
    list1(res);
    // 获取图片列表，即img标签列表
    let imgs = document.querySelectorAll('img');   
    lazyload(imgs);
    // 滚屏函数
    main.onscroll =function(){
        lazyload(imgs);
    }
})
function list1(res){
    res.forEach((item,index)=> {
        main.innerHTML += `
        <div class="content">
                <div class="left"><a href="./xiangqingye.html?id=${item.id}"><img data-src="${item.img.replace('/w.h','')}" src=""/></a></div>
                <div class="right">
                    <p><span>${item.nm}</span><span>${item.wish}人想看</span></p>
                    <p>${item.cat}</p>
                    <p><span>${item.desc}</span><span><a><i class="iconfont iconxihuan1"></i></a>想看</span></p>
                    <p>${item.rt}</p>
                </div>
        </div> `
    });
}

//第三页
//https://wx.maoyan.com/mmdb/movie/list/info.json?ci=20&movieIds=1352470,1211143,1396491,1320617,1302015,1218187,1250964,428,1188366,1229068
pAjax({
    url:'/my/movie/list/info.json',
    data:{
        ci:20,
        movieIds:"1352470,1211143,1396491,1320617,1302015,1218187,1250964,428,1188366,1229068",
    }
}).then(function(res){
    res = JSON.parse(res);
    res = res.data.movies;
    list2(res);
    let imgs = document.querySelectorAll('img');   
    lazyload(imgs);
    // 滚屏函数
    main.onscroll =function(){
        lazyload(imgs);
    }
})
function list2(res){
    res.forEach((item,index)=> {
        main.innerHTML += `
        <div class="content">
                <div class="left"><a href="./xiangqingye.html?id=${item.id}"><img data-src="${item.img.replace('/w.h','')}"/></a></div>
                <div class="right">
                    <p><span>${item.nm}</span><span>${item.wish}人想看</span></p>
                    <p>${item.cat}</p>
                    <p><span>${item.desc}</span><span><a><i class="iconfont iconxihuan1"></i></a>想看</span></p>
                    <p>${item.rt}</p>
                </div>
        </div>`
    });
}

// 懒加载
function lazyload(imgs){
    let h = window.innerHeight;
    let s = main.scrollTop
    for(let i=0;i<imgs.length;i++){
        if ((h+s)>imgs[i].offsetTop) {
            setTimeout(function(){
                let temp = new Image();
                temp.src = imgs[i].getAttribute('data-src');
                temp.onload = function(){
                imgs[i].src = imgs[i].getAttribute('data-src')
                }
            },1000)
        }
    }
}
